package com.ywl.study.service;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.transaction.ChainedTransactionManager;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
public class MySqlDBConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.ds.mysql.user")
    public DataSourceProperties mysqlUserDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    public DataSource mysqlUserDataSource() {
        return mysqlUserDataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
    }

    @Bean
    public JdbcTemplate mysqlUserJdbcTemplate(@Qualifier("mysqlUserDataSource") DataSource userDataSource) {
        return new JdbcTemplate(userDataSource);
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.ds.mysql.order")
    public DataSourceProperties mysqlOrderDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource mysqlOrderDataSource() {
        return mysqlOrderDataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
    }

    @Bean
    public JdbcTemplate mysqlOrderJdbcTemplate(@Qualifier("mysqlOrderDataSource") DataSource orderDataSource) {
        return new JdbcTemplate(orderDataSource);
    }


}
